function [hdrint,hdrdbl,X] = lrfmri_import(madfile,endian)
% lrfmri_import
%
%    [hdrint,hdrdbl,X] = lrfmri_import(madfile)    
%    [hdrint,hdrdbl,X] = lrfmri_import(madfile,endian)
%
%  output:
%       hdrint(1)  = Dx 
%       hdrint(2)  = Dy 
%       hdrint(3)  = Dz 
%       hdrint(4)  = SCANS
%       hdrint(5)  = EVENTS
%       hdrint(11) = 2 ! file format
%       hdrint(12) = fold
%       hdrint(13) = maxit
%       hdrint(14) = bfgsmem
%       hdrint(15) = mode
%       hdrint(16) = R
%       hdrint(17) = Ntau
%       hdrint(18) = blocks
%       hdrint(19) = trainsub
%       
%       hdrdbl(1)  = TR
%       hdrdbl(2)  = Vx
%       hdrdbl(3)  = Vy
%       hdrdbl(4)  = Vz
%       hdrdbl(9)  = lt
%       hdrdbl(10) = nut
%       hdrdbl(11) = cvauc
%       hdrdbl(12) = lambda
%       hdrdbl(13) = margin
%       hdrdbl(14) = Ttau(1)
%       hdrdbl(15) = Tspace
%       hdrdbl(16) = 0D0
%       hdrdbl(17) = cvloglik
%       hdrdbl(18) = datascale
%       hdrdbl(19) = kappa
%       hdrdbl(20) = slepwidth
% 
%       X : if folds>1, then Ey
%         : if folds=1, then classifier parameters. Parse them with
%                       FMRIPOD_PARCUBE
%
%   see also:
%
%       fmripod_parcube
%
%  Author: Mads Dyrholm

if nargin<2
  endian = 'n';
end

hdrsize = 20;

% open file for reading
[infid,msg] = fopen(madfile,'r',endian);
%int
fread(infid,1,'int32');
hdrint = fread(infid,20,'int32');
fread(infid,1,'int32');
%dbl
fread(infid,1,'int32');
hdrdbl = fread(infid,20,'double');
fread(infid,1,'int32');
%data 
sizeof_double = 8;
sizeof_int    = 4;
% read the data
elms = fread(infid,1,'int32') / sizeof_double;
X = fread(infid,elms,'double');
elms = fread(infid,1,'int32') / sizeof_double;
fclose(infid);


% fileversion stuff
switch hdrint(11)
 case 100
  X=reshape(X,[hdrint(5),hdrint(1),hdrint(2),hdrint(3),hdrint(17)]);
end
